package com.hancai.barrage.service.impl;

import com.hancai.barrage.mapper.BarrageMapper;
import com.hancai.barrage.model.Page;
import com.hancai.barrage.pagehelper.QueryObject;
import com.hancai.barrage.pojo.Barrage;
import com.hancai.barrage.service.CBarrageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 涵菜
 * @version 1.0
 * Autowired默认根据类型来装配
 * 弹幕的服务层
 */
@Service
public class BarrageServiceImpl implements CBarrageService {
    private  final BarrageMapper barrageMapper;

    public BarrageServiceImpl(BarrageMapper barrageMapper) {
        this.barrageMapper = barrageMapper;
    }

    @Override
    public void save(Barrage obj) {
        barrageMapper.insert(obj);
    }

    @Override
    public void updateById(Barrage obj) {
        barrageMapper.updateByPrimaryKey(obj);
    }

    @Override
    public void deleteById(Long id) {
        barrageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Barrage selectById(Long id) {
        return barrageMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Barrage> selectAll() {
        return barrageMapper.selectAll();
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        barrageMapper.changeStatus(id,status);
    }

    @Override
        public Page<Barrage> query(QueryObject qo) {
            Long total=barrageMapper.selectForCount(qo);
            if(total==0){
                return Page.empty(qo.getPageNum(),qo.getPageSize());
            }
            List<Barrage>list=barrageMapper.selectForList(qo);
            return new Page<>(qo.getPageNum(), qo.getPageSize(),total,list);
        }
}
